﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using NHibernate;
using Landscape.Business;
using Landscape.Business.FNH;
using Landscape.Business.FNH.Activities;
//using DCTM = Landscape.Business.Interface.Documentum;
using System.Configuration;
using System.Web;
using Landscape.Business.Entities;

namespace Landscape.WebApp.WCFServices
{
    [ServiceContract(Namespace = "Landscape")]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmergencyActions
    {
        [OperationContract]
        public Guid SaveEmergencyIncident(EmergencyIncident pIncident)
        {
                Guid eiID = Guid.Empty;
                using (var nhSession = NHSessionManager.GetManager().GetSession())
                {
                    using (var trans = nhSession.BeginTransaction())
                    {
                        EmergencyActivities ea = new EmergencyActivities(nhSession);
                        eiID = ea.SaveIncident(pIncident.ToBusinessObject(nhSession));
                        pIncident.ID = eiID;

                        if (pIncident.DctmFolderCode == null || pIncident.DctmFolderCode.Trim() == string.Empty)
                        {
                            var dctmID = CreateDCTMFolder(eiID);
                            if (dctmID.Trim() == string.Empty)
                            {
                                trans.Rollback();
                                throw new Exception("Cannot create the folder. Incident cannot be saved.");
                            }

                            pIncident.DctmFolderCode = dctmID;
                            ea.SaveIncident(pIncident.ToBusinessObject(nhSession));
                        }

                        trans.Commit();
                    }
            }

            return eiID;
        }

        private string CreateDCTMFolder(Guid pIncID)
        {
            /* var sm = DCTM.SessionManager.GetManager();
            var token = sm.CreateTPOTAdminSession();

            var fldrID = DCTM.Actions.GetFolderDCTMID(ConfigurationManager.AppSettings["DCTM_ERM_Loc"], token);
            if (fldrID.Trim() != string.Empty)
            {
                DCTM.Folder fldr = new DCTM.Folder(token, fldrID);
                //if (fldr.Folders.Count > 0 && fldr.Folders.Where(Exp => Exp.Name == pIncID.ToString()).Count() == 0)
                if (fldr.Folders.Where(Exp => Exp.Name == pIncID.ToString()).Count() == 0)
                {
                    fldr.CreateFolder(pIncID.ToString());
                    fldr.Refresh();
                }

                return fldr.Folders.Where(Exp => Exp.Name == pIncID.ToString()).Single().DctmID;                
            }*/

            return string.Empty;
        }

        [OperationContract]
        public List<EmergencyIncident> GetEmergencyIncidentsByTitle(string pTitle, int pPageNo, int pPageSize)
        {
            List<EmergencyIncident> eiList = new List<EmergencyIncident>();
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                foreach (var item in ea.GetEmergencyIncidentsByTitle(pTitle, pPageNo, pPageSize))
                {
                    eiList.Add(new EmergencyIncident(item));
                }
            }

            return eiList;
        }

        [OperationContract]
        public EmergencyIncident GetEmergencyIncidentDetailsByID(Guid pIncCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                return new EmergencyIncident(ea.GetIncidentByCode(pIncCode));
            }
        }

        [OperationContract]
        public List<StatusBoardItem> GetStatusBoardItemsByTitle(string pTitle, int pPageNo, int pPageSize)
        {
             List<StatusBoardItem> eiList = new List<StatusBoardItem>();
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                foreach (var item in ea.GetStatusBoardItemsByTitle(pTitle, pPageNo, pPageSize))
                {
                    eiList.Add(new StatusBoardItem(item));
                }
            }

            return eiList;
        }

        [OperationContract]
        public void SaveStatusUpdate(Guid pStatusItemCode, Guid pIncidentCode, string pMessage, Guid pUserCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    EmergencyActivities ea = new EmergencyActivities(nhSession);
                    ea.SaveStatusUpdate(pStatusItemCode, pIncidentCode, pMessage, pUserCode);

                    trans.Commit();
                }
            }
        }

        [OperationContract]
        public Scribe UpdateScribe(Guid pIncidentCode, Guid pScribeCode, string pMsg, Guid pUserCode)
        {
            var incident = this.GetEmergencyIncidentDetailsByID(pIncidentCode);
            if (incident.ScribeLog.Count > 0)
            {
                if (incident.ScribeLog.Where(exp => exp.ID == pScribeCode).Count() > 0)
                {
                    incident.ScribeLog.Where(exp => exp.ID == pScribeCode).Single().Message = pMsg;
                    incident.ScribeLog.Where(exp => exp.ID == pScribeCode).Single().UpdatedBy = pUserCode;
                    incident.ScribeLog.Where(exp => exp.ID == pScribeCode).Single().UpdatedDate = DateTime.Now;

                    this.SaveEmergencyIncident(incident);

                    return incident.ScribeLog.Where(exp => exp.ID == pScribeCode).Single();
                }
            }

            return null;
        }

        [OperationContract]
        public void CommitFiles(List<Attachment> pFilesList, Guid pIncID)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    foreach (Attachment item in pFilesList)
                    {
                        EmergencyActivities ea = new EmergencyActivities(nhSession);
                        var filePath = HttpContext.Current.Server.MapPath("~/TempFiles") + "\\" + item.FileName;
                        ea.SaveFile(pIncID, filePath, item.FileName, string.Empty, item.FileSize, item.UploadedBy);
                    }

                    trans.Commit();  
                }
            }
        }

        [OperationContract]
        public List<Attachment> GetFilesForIncident(Guid pIncID, int pPageNo, int pPageSize)
        {
            List<Attachment> fileAttachments = new List<Attachment>();
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                var inc = ea.GetIncidentByCode(pIncID);
                foreach (var att in (pPageSize != -1 ? inc.Attachments.Skip(pPageNo * pPageSize).Take(pPageSize) : inc.Attachments))
                {
                    fileAttachments.Add(new Attachment(att));
                }
            }

            return fileAttachments;
        }

        [OperationContract]
        public Guid SaveFeedback(Guid pIncCode, Feedback pFeed)
        {
            Guid eiID = Guid.Empty;
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                using (var trans = nhSession.BeginTransaction())
                {
                    EmergencyActivities ea = new EmergencyActivities(nhSession);
                    eiID = ea.SaveFeedback(pIncCode, pFeed.ToBusinessObject(nhSession));                    

                    trans.Commit();
                }
            }

            return eiID;
        }

        [OperationContract]
        public List<Feedback> GetFeedbackList(Guid pIncID, string pFeedbackType, int pPageNo, int pPageSize)
        {
            List<Feedback> feedbackList = new List<Feedback>();
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                var inc = ea.GetIncidentByCode(pIncID);
                IEnumerable<EmergencyFeedback> fbList = null;
                if (pFeedbackType.Trim() == "Positives")
                {
                    fbList = inc.Feedback.Where(exp => exp.Type == FeedbackType.POSITIVES);
                }
                else if (pFeedbackType.Trim() == "Corrective Actions")
                {
                    fbList = inc.Feedback.Where(exp => exp.Type == FeedbackType.CORRECTIVE_ACTION);
                }
                else
                {
                    fbList = inc.Feedback;
                }

                foreach (var att in (pPageSize != -1 ? fbList.Skip(pPageNo * pPageSize).Take(pPageSize) : fbList))
                {
                    feedbackList.Add(new Feedback(att));
                }
            }

            return feedbackList;
        }
        
        [OperationContract]
        public void SubmitFeedbackAndClose(Guid pIncCode, Guid pUserCode)
        {
            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                EmergencyActivities ea = new EmergencyActivities(nhSession);
                using (var trans = nhSession.BeginTransaction())
                {
                    ea.SubmitFeedbackAndClose(pIncCode, pUserCode);
                    trans.Commit();
                }
            }            
        }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmergencyIncident
    {
        public EmergencyIncident()
        {
            this.ID = Guid.Empty;
            this.ScribeLog = new List<Scribe>();
            this.StatusBoardLog = new List<StatusBoardEntry>();
        }

        public EmergencyIncident(Landscape.Business.Entities.EmergencyIncident pBusinessEntity)
        {
            this.ID = pBusinessEntity.ID;
            this.Title = pBusinessEntity.Title;
            this.FirstIncidentReportTime = DateTime.SpecifyKind(pBusinessEntity.FirstIncidentReportTime, DateTimeKind.Local);
            this.IsIncident = pBusinessEntity.IsIncident;
            this.IsClosed = pBusinessEntity.IsClosed;
            this.ClosedBy = pBusinessEntity.ClosedBy;
            this.CloseComment = pBusinessEntity.CloseComment;
            this.IsDeleted = pBusinessEntity.IsDeleted;
            this.CreatedBy = pBusinessEntity.CreatedBy;
            this.CreatedDate = pBusinessEntity.CreatedDate;
            this.UpdatedBy = pBusinessEntity.UpdatedBy;
            this.UpdatedDate = pBusinessEntity.UpdatedDate;
            this.VesselCode = pBusinessEntity.Vessel.ID;
            this.ClosedDate = pBusinessEntity.ClosedDate;
            this.VesselShortName = pBusinessEntity.Vessel.ShortName;
            this.DctmFolderCode = pBusinessEntity.DctmFolderCode;

            this.ScribeLog = new List<Scribe>();
            if (pBusinessEntity.ScribeLog != null && pBusinessEntity.ScribeLog.Count > 0)
            {
                foreach (var scribe in pBusinessEntity.ScribeLog)
                {
                    this.ScribeLog.Add(new Scribe(scribe));
                }
            }

            this.StatusBoardLog = new List<StatusBoardEntry>();
            if (pBusinessEntity.StatusBoardLog != null && pBusinessEntity.StatusBoardLog.Count > 0)
            {
                foreach (var entry in pBusinessEntity.StatusBoardLog)
                {
                    this.StatusBoardLog.Add(new StatusBoardEntry(entry));
                }
            }

            this.VesselName = pBusinessEntity.Vessel.Name;
            this.FRTime = pBusinessEntity.FirstIncidentReportTime.ToString("dd-MMM-yyyy HH:mm");

            if (pBusinessEntity.IsClosed)
            {
                AdminActivities adminActs = new AdminActivities();
                var uDet = adminActs.GetUserMasterByID(pBusinessEntity.ClosedBy);
                this.ClosedUserName = uDet.FirstName + " " + uDet.LastName;
            }
            else
            {
                this.ClosedUserName = "-";
            }
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Title
        { get; set; }

        [DataMember]
        public DateTime FirstIncidentReportTime
        { get; set; }

        [DataMember]
        public string FRTime
        { get; set; }

        [DataMember]
        public bool IsIncident
        { get; set; }

        [DataMember]
        public bool IsClosed
        { get; set; }

        [DataMember]
        public DateTime? ClosedDate
        { get; set; }

        [DataMember]
        public Guid ClosedBy
        { get; set; }

        [DataMember]
        public string ClosedUserName
        { get; set; }

        [DataMember]
        public string CloseComment
        { get; set; }

        [DataMember]
        public bool IsDeleted
        { get; set; }

        [DataMember]
        public IList<Scribe> ScribeLog
        { get; set; }

        [DataMember]
        public IList<StatusBoardEntry> StatusBoardLog
        { get; set; }

        [DataMember]
        public Guid VesselCode
        { get; set; }

        [DataMember]
        public string VesselName
        { get; set; }

        [DataMember]
        public string VesselShortName
        { get; set; }

        [DataMember]
        public string DctmFolderCode
        { get; set; }

        [DataMember]
        public Guid CreatedBy
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid UpdatedBy
        { get; set; }

        [DataMember]
        public DateTime UpdatedDate
        { get; set; }

        public Landscape.Business.Entities.EmergencyIncident ToBusinessObject(ISession pSession)
        {
            Business.Entities.EmergencyIncident bizObject = null;
            EmergencyActivities emergencyActivities = new EmergencyActivities(pSession);
            AdminActivities adminActs = new AdminActivities(pSession);

            if (this.ID != Guid.Empty)
            {
                bizObject = emergencyActivities.GetIncidentByCode(this.ID);
            }
            else
            {
                bizObject = new Business.Entities.EmergencyIncident();
            }

            bizObject.CloseComment = this.CloseComment;
            bizObject.ClosedBy = this.ClosedBy;
            bizObject.ClosedDate = this.ClosedDate;
            bizObject.CreatedBy = this.CreatedBy;
            bizObject.CreatedDate = this.CreatedDate;
            bizObject.FirstIncidentReportTime = this.FirstIncidentReportTime.ToLocalTime();
            bizObject.IsClosed = this.IsClosed;
            bizObject.IsDeleted = this.IsDeleted;
            bizObject.IsIncident = this.IsIncident;
            bizObject.Title = this.Title;
            bizObject.UpdatedBy = this.UpdatedBy;
            bizObject.UpdatedDate = this.UpdatedDate;
            bizObject.Vessel = adminActs.GetVesselByCode(this.VesselCode);
            bizObject.DctmFolderCode = this.DctmFolderCode;

            //If current object has no scribe log and business object has scribe log in database then clear all
            if ((this.ScribeLog == null || this.ScribeLog.Count == 0) && (bizObject.ScribeLog != null && bizObject.ScribeLog.Count > 0))
                bizObject.ScribeLog.Clear();

            if (this.ScribeLog != null && this.ScribeLog.Count > 0)
            {
                foreach (var item in this.ScribeLog)
                {
                    bizObject.ScribeLog.Add(item.ToBusinessObject(pSession));
                }
            }

            if ((this.StatusBoardLog == null || this.StatusBoardLog.Count == 0) && (bizObject.StatusBoardLog != null && bizObject.StatusBoardLog.Count > 0))
                bizObject.StatusBoardLog.Clear();

            if (this.ScribeLog != null && this.ScribeLog.Count > 0)
            {
                foreach (var item in this.StatusBoardLog)
                {
                    bizObject.StatusBoardLog.Add(item.ToBusinessObject(pSession));
                }
            }

            return bizObject;
        }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Scribe
    {
        public Scribe()
        {
            this.ID = Guid.Empty;
        }

        public Scribe(Business.Entities.Scribe pBusinessObject)
        {
            // TODO: Complete member initialization
            this.ID = pBusinessObject.ID;
            this.ScribeIndex = pBusinessObject.ScribeIndex;
            this.Message = pBusinessObject.Message;
            this.CreatedBy = pBusinessObject.CreatedBy;
            this.CreatedDate = pBusinessObject.CreatedDate;
            this.UpdatedBy = pBusinessObject.UpdatedBy;
            this.UpdatedDate = pBusinessObject.UpdatedDate;
            this.MessageType = pBusinessObject.MessageType.ToString();
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public int ScribeIndex
        { get; set; }

        [DataMember]
        public string Message
        { get; set; }

        [DataMember]
        public string MessageType
        { get; set; }

        [DataMember]
        public Guid CreatedBy
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid UpdatedBy
        { get; set; }

        [DataMember]
        public DateTime UpdatedDate
        { get; set; }

        public Business.Entities.Scribe ToBusinessObject(ISession pSession)
        {
            Business.Entities.Scribe bizObject = null;
            Landscape.Business.FNH.Activities.EmergencyActivities emergencyActivities = new Landscape.Business.FNH.Activities.EmergencyActivities(pSession);

            if (this.ID != Guid.Empty)
            {
                bizObject = emergencyActivities.GetScribeByCode(this.ID);
            }
            else
            {
                bizObject = new Business.Entities.Scribe();
            }

            bizObject.Message = this.Message;
            bizObject.ScribeIndex = this.ScribeIndex;
            bizObject.UpdatedBy = this.UpdatedBy;
            bizObject.UpdatedDate = this.UpdatedDate;
            bizObject.CreatedBy = this.CreatedBy;
            bizObject.CreatedDate = this.CreatedDate;
            bizObject.MessageType = (ScribeType)Enum.Parse(typeof(ScribeType), this.MessageType);

            return bizObject;
        }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class StatusBoardItem
    {
        public StatusBoardItem()
        { this.ID = Guid.Empty; }

        public StatusBoardItem(Landscape.Business.Entities.StatusBoardItem pBusinessObject)
        {
            this.ID = pBusinessObject.ID;
            this.Title = pBusinessObject.Title;
            this.ShortName = pBusinessObject.ShortName;
            this.IsDeleted = pBusinessObject.IsDeleted;
            this.Sequence = pBusinessObject.Sequence;
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Title
        { get; set; }

        [DataMember]
        public string ShortName
        { get; set; }

        [DataMember]
        public int Sequence
        { get; set; }

        [DataMember]
        public bool IsDeleted
        { get; set; }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class StatusBoardEntry
    {
        public StatusBoardEntry()
        { this.ID = Guid.Empty; }

        public StatusBoardEntry(Landscape.Business.Entities.StatusBoardEntry pBusinessEntity)
        {
            this.ID = pBusinessEntity.ID;
            this.StatusItemCode = pBusinessEntity.StatusItem.ID;
            this.StatusEntry = pBusinessEntity.StatusEntry;
            this.CreatedBy = pBusinessEntity.CreatedBy;
            this.CreatedDate = pBusinessEntity.CreatedDate;
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public Guid StatusItemCode
        { get; set; }
        
        [DataMember]
        public string StatusEntry
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid CreatedBy
        { get; set; }

        public Landscape.Business.Entities.StatusBoardEntry ToBusinessObject(ISession pSession)
        {
            Business.Entities.StatusBoardEntry bizObject = null;
            EmergencyActivities emergencyActivities = new EmergencyActivities(pSession);
            AdminActivities adminActs = new AdminActivities(pSession);

            if (this.ID != Guid.Empty)
            {
                bizObject = emergencyActivities.GetStatusBoardEntryByCode(this.ID);
            }
            else
            {
                bizObject = new Business.Entities.StatusBoardEntry();
            }

            bizObject.CreatedBy = this.CreatedBy;
            bizObject.CreatedDate = this.CreatedDate;
            bizObject.StatusEntry = this.StatusEntry;
            bizObject.StatusItem = emergencyActivities.GetStatusItemByCode(this.StatusItemCode);

            return bizObject;
        }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Attachment
    {
        public Attachment(Business.Entities.FileAttachment pAttchment)
        {
            // TODO: Complete member initialization
            this.ID = pAttchment.ID;
            this.Title = pAttchment.Title;
            this.Description = pAttchment.Description;
            this.FileName = pAttchment.FileName;
            this.FileSize = pAttchment.FileSize;
            this.UploadedBy = pAttchment.UploadBy;
            this.UploadDate = pAttchment.UploadDate;
            this.RepositoryCode = pAttchment.RepositoryCode;

            AdministrativeActions aa = new AdministrativeActions();
            var ud = aa.GetUserByID(this.UploadedBy);
            this.UploadUserName = ud.FirstName + " " + ud.LastName;

            if (UploadDate.HasValue)
            {
                this.UploadDateString = UploadDate.Value.ToString("dd-MMM-yyyy");
            }
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Title
        { get; set; }

        [DataMember]
        public string Description
        { get; set; }

        [DataMember]
        public string FileName
        { get; set; }

        [DataMember]
        public long FileSize
        { get; set; }

        [DataMember]
        public Guid UploadedBy
        { get; set; }

        [DataMember]
        public DateTime? UploadDate
        { get; set; }

        [DataMember]
        public string UploadDateString
        { get; set; }

        [DataMember]
        public string RepositoryCode
        { get; set; }

        [DataMember]
        public string UploadUserName
        { get; set; }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Feedback
    {
        public Feedback()
        { this.ID = Guid.Empty; }

        public Feedback(Business.Entities.EmergencyFeedback pBusinessObject)
        {
            this.ID = pBusinessObject.ID;
            this.FeedbackType = pBusinessObject.Type.ToString();
            this.FeedbackComments = pBusinessObject.Comments;
            this.CorrectiveActionDescription = pBusinessObject.CorrectiveAction;
            this.AssignedTo = pBusinessObject.AssignedTo.ID;
            this.Reviewer = pBusinessObject.ReviewTo.ID;
            this.ExpectedDate = pBusinessObject.ResponseDueDate;
            this.CorrectiveActionStatus = (pBusinessObject.Type == Business.FeedbackType.CORRECTIVE_ACTION ? (pBusinessObject.CorrectiveActionInstance != null ? pBusinessObject.CorrectiveActionInstance.Status.ToString() : "-") : "-");
            this.Status = pBusinessObject.Status.ToString();
            this.CreatedBy = pBusinessObject.CreatedBy;
            this.CreatedDate = pBusinessObject.CreatedDate;
            this.UpdatedBy = pBusinessObject.UpdatedBy;
            this.UpdatedDate = pBusinessObject.UpdatedDate;

            AdministrativeActions aa = new AdministrativeActions();
            var ud = aa.GetUserByID(this.CreatedBy);
            this.CreatedUser = ud.FirstName + " " + ud.LastName;

            if (this.AssignedTo.HasValue && this.AssignedTo.Value != Guid.Empty)
            {
                ud = aa.GetUserByID(this.AssignedTo.Value);
                this.AssignedUser = ud.FirstName + " " + ud.LastName;
            }
            else
            {
                this.AssignedUser = "-";
            }

            if (this.Reviewer.HasValue && this.Reviewer.Value != Guid.Empty)
            {
                ud = aa.GetUserByID(this.Reviewer.Value);
                this.ReviewUser = ud.FirstName + " " + ud.LastName;
            }
            else
            {
                this.ReviewUser = "-";
            }

            this.CorrectiveActionLink = "-";
            if (pBusinessObject.CorrectiveActionInstance != null)
            {
                BasePage bp = new BasePage();
                switch (this.CorrectiveActionStatus)
                {
                    case "NEW":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> Draft</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "INPROGRESS":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> For Response</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "WAIT_REVIEW":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> For Review</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "REWORK":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> For Rework</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "WAIT_HSEQ":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> For HSEQ Approval</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "WAIT_OD":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> For OD Approval</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }

                    case "CLOSED":
                        {
                            this.CorrectiveActionLink = string.Format("<a href='#' onclick='javaScript:window.open(ResolveUrl(\"{0}\"));return false;'> Closed</a>", "~/Internal/Operations/CorrectiveAction.aspx" + "?AID=" + pBusinessObject.CorrectiveActionInstance.ID.ToString());
                            break;
                        }
                }
            }
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string FeedbackType
        { get; set; }

        [DataMember]
        public string FeedbackComments
        { get; set; }

        [DataMember]
        public string CorrectiveActionDescription
        { get; set; }

        [DataMember]
        public Guid? AssignedTo
        { get; set; }

        [DataMember]
        public Guid? Reviewer
        { get; set; }

        [DataMember]
        public DateTime? ExpectedDate
        { get; set; }

        [DataMember]
        public string CorrectiveActionStatus
        { get; set; }

        [DataMember]
        public string CorrectiveActionLink
        { get; set; }

        [DataMember]
        public string Status
        { get; set; }
        
        [DataMember]
        public Guid CreatedBy
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid UpdatedBy
        { get; set; }

        [DataMember]
        public DateTime UpdatedDate
        { get; set; }

        [DataMember]
        public string CreatedUser
        { get; set; }

        [DataMember]
        public string AssignedUser
        { get; set; }

        [DataMember]
        public string ReviewUser
        { get; set; }

        public Business.Entities.EmergencyFeedback ToBusinessObject(ISession pSession)
        { 
            Business.Entities.EmergencyFeedback bizObject = null;

            if (this.ID == Guid.Empty)
            {
                bizObject = new Business.Entities.EmergencyFeedback();
                bizObject.CreatedDate = DateTime.Now;
                bizObject.CreatedBy = this.CreatedBy;
                bizObject.Status = FeedbackStatus.OPEN;
            }
            else
            {
                EmergencyActivities ea = new EmergencyActivities(pSession);
                bizObject = ea.GetEmergencyFeedbackByID(this.ID);
            }

            bizObject.Comments = this.FeedbackComments;
            bizObject.CorrectiveAction = this.CorrectiveActionDescription;

            if (this.AssignedTo.HasValue)
                bizObject.AssignedTo = new Business.Entities.UserMaster() { ID = this.AssignedTo.Value };

            if (this.Reviewer.HasValue)
                bizObject.ReviewTo = new Business.Entities.UserMaster() { ID = this.Reviewer.Value };

            if (this.ExpectedDate.HasValue)
                bizObject.ResponseDueDate = this.ExpectedDate.Value;

            bizObject.Type = (Business.FeedbackType)Enum.Parse(typeof(Business.FeedbackType), this.FeedbackType);
            bizObject.Status = (FeedbackStatus)Enum.Parse(typeof(FeedbackStatus), this.Status);

            bizObject.UpdatedBy = this.UpdatedBy;
            bizObject.UpdatedDate = DateTime.Now;

            return bizObject;
        }
    }
}
